<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="corporationName">
        <el-input v-model="queryParams.corporationName" placeholder="请输入企业名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="corporationUscId" label-width="130px">
        <el-input v-model="queryParams.corporationUscId" placeholder="请输入统一社会信用代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="企业超级管理员姓名" prop="adminName">
        <el-input v-model="queryParams.adminName" placeholder="请输入企业超级管理员姓名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="身份证号" prop="adminIdNumber">
        <el-input v-model="queryParams.adminIdNumber" placeholder="请输入身份证号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="登录手机号" prop="loginMobile">
        <el-input v-model="queryParams.loginMobile" placeholder="请输入登录手机号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="销售订单号" prop="saleTicketCode">
        <el-input v-model="queryParams.saleTicketCode" placeholder="请输入销售订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="版本号" prop="productVersion">
        <el-input v-model="queryParams.productVersion" placeholder="请输入版本号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="授权有效期类型" prop="authType">
        <el-select v-model="queryParams.authType" placeholder="请选择授权有效期类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="授权有效期开始时间" prop="authDurationBegin">
        <el-input v-model="queryParams.authDurationBegin" placeholder="请输入授权有效期开始时间" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="授权有效期结束时间" prop="authDurationEnd">
        <el-input v-model="queryParams.authDurationEnd" placeholder="请输入授权有效期结束时间" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="授权IP范围（0:无限制，1:指定范围）" prop="authIpType">
        <el-select v-model="queryParams.authIpType" placeholder="请选择授权IP范围（0:无限制，1:指定范围）" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="license文件" prop="licenseFileCode">
        <el-input v-model="queryParams.licenseFileCode" placeholder="请输入license文件" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item> -->
      <el-form-item>
        <el-button v-preventReClick type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button v-preventReClick icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-preventReClick type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddFunc"
                   v-hasPermi="['hbcsupport:hbc-client-auth:create']">新增业务授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-preventReClick type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['hbcsupport:hbc-client-auth:create']">新增许可授权</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-preventReClick type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['hbcsupport:hbc-client-auth:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="企业名称" align="center" prop="corporationName" />
      <el-table-column label="统一社会信用代码" align="center" prop="corporationUscId" />
      <!-- <el-table-column label="企业超级管理员姓名" align="center" prop="adminName" /> -->
      <!-- <el-table-column label="身份证号" align="center" prop="adminIdNumber" /> -->
      <!-- <el-table-column label="登录手机号" align="center" prop="loginMobile" /> -->
      <!-- <el-table-column label="销售订单号" align="center" prop="saleTicketCode" /> -->
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="版本号" align="center" prop="productVersion" />
      <el-table-column label="授权有效期" align="center" prop="authType" width="170">
        <template v-slot="scope">
          <span word>{{ scope.row.authType==1 ? parseTime(scope.row.authDurationBegin,'{y}.{m}.{d}')+' - '+parseTime(scope.row.authDurationEnd,'{y}.{m}.{d}'):'无限制' }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="授权有效期开始时间" align="center" prop="authDurationBegin" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.authDurationBegin) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="授权有效期结束时间" align="center" prop="authDurationEnd" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.authDurationEnd) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="授权IP范围（0:无限制，1:指定范围）" align="center" prop="authIpType" /> -->
      <!-- <el-table-column label="license文件" align="center" prop="licenseFileCode" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="创建人" align="center" prop="creatorName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-preventReClick size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['hbcsupport:hbc-client-auth:update']">修改</el-button>
          <el-button v-preventReClick size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['hbcsupport:hbc-client-auth:delete']">删除</el-button>
          <el-button v-preventReClick size="mini" type="text" icon="el-icon-edit" @click="handleViewOrUpdateFunc(scope.row)"
                     v-hasPermi="['hbcsupport:hbc-client-auth:update']">修改业务授权</el-button>
          <el-button v-preventReClick size="mini" type="text" icon="el-icon-edit" @click="handleViewOrUpdateLicense(scope.row)"
                     v-hasPermi="['hbcsupport:hbc-client-auth:update']">查看License</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="160px">
        <el-row>
          <el-col :span=12>
            <el-form-item label="企业名称" prop="corporationName">
              <el-input v-model="form.corporationName" placeholder="请输入企业名称" />
            </el-form-item>
          </el-col>
          <el-col :span=12>
            <el-form-item label="统一社会信用代码" prop="corporationUscId">
              <el-input v-model="form.corporationUscId" placeholder="请输入统一社会信用代码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span=12>
            <el-form-item label="企业超级管理员姓名" prop="adminName">
              <el-input v-model="form.adminName" placeholder="请输入企业超级管理员姓名" />
            </el-form-item>
          </el-col>
          <el-col :span=12>
            <el-form-item label="身份证号" prop="adminIdNumber">
              <el-input v-model="form.adminIdNumber" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span=12>
            <el-form-item label="登录手机号" prop="loginMobile">
              <el-input v-model="form.loginMobile" placeholder="请输入登录手机号" />
            </el-form-item>
          </el-col>
          <el-col :span=12>
            <el-form-item label="销售订单号" prop="saleTicketCode">
              <el-input v-model="form.saleTicketCode" placeholder="请输入销售订单号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span=12>
            <el-form-item label="产品名称" prop="productName">
              <!-- <el-input v-model="form.productName" placeholder="请输入产品名称" /> -->
              <el-select clearable v-model="form.productName" filterable placeholder="请选择">
                <el-option v-for="item in hbcProductNameList.filter(item=>item.productType=='HBC_PRODUCT_TYPE_SYS')" :key="item.productName" :label="item.productName" :value="item.productName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span=12>
            <el-form-item label="版本号" prop="productVersion">
              <!-- <el-input v-model="form.productVersion" placeholder="请输入版本号" /> -->
              <el-select clearable v-model="form.productVersion" filterable placeholder="请选择">
                <el-option v-for="item in hbcProductVersionList.filter(item=>item.productType=='HBC_PRODUCT_TYPE_SYS')" :key="item.productVersion" :label="item.productVersion" :value="item.productVersion" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span=12>
            <el-form-item label="授权有效期类型" prop="authType">
              <!-- <el-select v-model="form.authType" placeholder="请选择授权有效期类型">
                <el-option label="请选择字典生成" value="" />
              </el-select> -->
              <el-radio-group v-model="form.authType">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.HBC_PRODUCT_SCOPE_TYPE)"
                          :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>

          </el-col>
          <el-col :span=12>
            <!-- <el-form-item label="授权有效期开始时间" prop="authDurationBegin">
              <el-input v-model="form.authDurationBegin" placeholder="请输入授权有效期开始时间" />
            </el-form-item> -->
            <!-- <el-form-item label="授权有效期结束时间" prop="authDurationEnd">
              <el-input v-model="form.authDurationEnd" placeholder="请输入授权有效期结束时间" />
            </el-form-item> -->
            <!-- <el-date-picker v-model="form.authDuration" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" /> -->

            <el-date-picker
              v-model="form.authDuration"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              :default-time="['00:00:00', '23:59:59']"
              @input="selectTime"
            />
            <!-- <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" /> -->
            <!-- <el-input v-model="form.authDurationBegin" placeholder="请输入授权有效期开始时间" />
            <el-input v-model="form.authDurationEnd" placeholder="请输入授权有效期结束时间" /> -->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span=12>
            <el-form-item label="授权IP范围" prop="authIpType">
              <!-- <el-select v-model="form.authIpType" placeholder="请选择授权IP范围（0:无限制，1:指定范围）">
                <el-option label="请选择字典生成" value="" />
              </el-select> -->
              <el-radio-group v-model="form.authIpType">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.HBC_PRODUCT_SCOPE_TYPE)"
                          :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span=12>
          </el-col>
        </el-row>
        <app-ip-range-list :ipRangeList="form.hbcClientAuthIpRangeList"></app-ip-range-list>
        <!-- <el-form-item label="license文件" prop="licenseFileCode">
          <el-input v-model="form.licenseFileCode" placeholder="请输入license文件" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick type="primary" @click="submitForm">确 定</el-button>
        <el-button v-preventReClick @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <app-function-list
      :config="appFunctionListConfig"
      ref="appFunctionListRef"
    >
    </app-function-list>

    <app-license-list
      :config="appLicenseListConfig"
      ref="appLicenseListRef">
    </app-license-list>

  </div>
</template>

<script>
import { createHbcClientAuth, updateHbcClientAuth, deleteHbcClientAuth, getHbcClientAuth, getHbcClientAuthPage, exportHbcClientAuthExcel, getHbcClientAuthListAll } from "@/api/hbcsupport/hbcClientAuth";

import { getHbcProductListAll } from "@/api/hbcsupport/hbcProduct";

// import { parseTime } from './ruoyi'

import AppIpRangeList from "./app-ip-range-list";
import AppLicenseList from "./app-license-list";
import AppFunctionList from "./app-function-list"


export default {
  name: "HbcClientAuth",
  components: {
    AppIpRangeList,
    AppLicenseList,
    AppFunctionList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 混合云支撑-混合云客户授权管理列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        corporationName: null,
        corporationUscId: null,
        adminName: null,
        adminIdNumber: null,
        loginMobile: null,
        saleTicketCode: null,
        productName: null,
        productVersion: null,
        authType: null,
        authDurationBegin: null,
        authDurationEnd: null,
        authIpType: null,
        licenseFileCode: null,
        createTime: [],
      },
      // 表单参数
      form: {
        hbcClientAuthIpRangeList: [],
      },
      // 表单校验
      rules: {
        corporationName: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
        corporationUscId: [{ required: true, message: "统一社会信用代码不能为空", trigger: "blur" }],
        adminName: [{ required: true, message: "企业超级管理员姓名不能为空", trigger: "blur" }],
        adminIdNumber: [{ required: true, message: "身份证号不能为空", trigger: "blur" }],
        loginMobile: [{ required: true, message: "登录手机号不能为空", trigger: "blur" }],
        // saleTicketCode: [{ required: true, message: "销售订单号不能为空", trigger: "blur" }],
        productName: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
        productVersion: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
        authType: [{ required: true, message: "授权有效期类型不能为空", trigger: "change" }],
        // authDurationBegin: [{ required: true, message: "授权有效期开始时间不能为空", trigger: "blur" }],
        // authDurationEnd: [{ required: true, message: "授权有效期结束时间不能为空", trigger: "blur" }],
        authIpType: [{ required: true, message: "授权IP范围（0:无限制，1:指定范围）不能为空", trigger: "change" }],
        // licenseFileCode: [{ required: true, message: "license文件不能为空", trigger: "blur" }],
      },
      hbcProductNameList:[],
      hbcProductVersionList:[],

      //业务授权对话框
      appFunctionListConfig:{
        hbcClientAuthId:undefined,
        hbcClientAuthIdFixed:false,
        titleFunc: "",
        openFunc: false,
        queryParams: {
          pageNo: 1,
          pageSize: 100
        }
      },

      //License对话框
      appLicenseListConfig:{
        hbcClientAuthId:undefined,
        titleLicense:"",
        openLicense: false,
        queryParams: {
          pageNo: 1,
          pageSize: 10
        }
      }

    };
  },
  created() {
    this.getList();
    this.getHbcProductListAll();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getHbcClientAuthPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getHbcProductListAll(){
      getHbcProductListAll({}).then(response => {
        const hbcProductNameListMap = new Map();
        const hbcProductVersionMap = new Map();
        const hbcProductNameList = [];
        const hbcProductVersionList = [];
        response.data.forEach(item => {
          if (!hbcProductNameListMap.has(item.productName)) {
            hbcProductNameListMap.set(item.productName, true)
            hbcProductNameList.push(item);
          }
          if (!hbcProductVersionMap.has(item.productVersion)) {
            hbcProductVersionMap.set(item.productVersion, true)
            hbcProductVersionList.push(item);
          }
        });
        // 上面去重
        this.hbcProductNameList = response.data;
        this.hbcProductVersionList = response.data;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        corporationName: undefined,
        corporationUscId: undefined,
        adminName: undefined,
        adminIdNumber: undefined,
        loginMobile: undefined,
        saleTicketCode: undefined,
        productName: undefined,
        productVersion: undefined,
        authType: undefined,
        authDurationBegin: undefined,
        authDurationEnd: undefined,
        authIpType: undefined,
        licenseFileCode: undefined,
        authDuration:undefined,
        hbcClientAuthIpRangeList:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加许可授权";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getHbcClientAuth(id).then(response => {
        this.form = response.data;
        let authDurationArr = [];
        if(this.form.authDurationBegin){
          authDurationArr.push(new Date(this.form.authDurationBegin));
        }
        if(this.form.authDurationEnd){
          authDurationArr.push(new Date(this.form.authDurationEnd));
        }
        this.form.authDuration=authDurationArr;
        this.open = true;
        this.title = "修改许可授权";
        // console.log("this.form->",this.form);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        if(this.form.authDuration){
          // LocalDateTime字段需要传送毫秒数 XXX TODO
          this.form.authDurationBegin=new Date(this.form.authDuration[0]).getTime();
          this.form.authDurationEnd=new Date(this.form.authDuration[1]).getTime();
        }
        // 修改的提交
        if (this.form.id != null) {
          updateHbcClientAuth(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createHbcClientAuth(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除混合云支撑-混合云客户授权管理编号为"' + id + '"的数据项?').then(function() {
          return deleteHbcClientAuth(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有混合云支撑-混合云客户授权管理数据项?').then(() => {
          this.exportLoading = true;
          return exportHbcClientAuthExcel(params);
        }).then(response => {
          this.$download.excel(response, '混合云支撑-混合云客户授权管理.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    selectTime(data) {
      this.$nextTick(() => {
        this.$set(this.form, 'time', [data[0], data[1]]);
      });
    },

    //==== 添加业务授权对话框
    handleAddFunc(){
      // console.log("handleAddFunc:");
      // // XXX TODO subview
      this.$refs.appFunctionListRef.resetFunc();
      this.appFunctionListConfig.openFunc = true;
      this.appFunctionListConfig.titleFunc = `添加业务授权`;
      this.appFunctionListConfig.hbcClientAuthIdFixed=false;
    },
    handleViewOrUpdateFunc(row) {
      // console.log("handleViewOrUpdateFunc:",row);
      this.appFunctionListConfig.openFunc = true;
      this.appFunctionListConfig.titleFunc = `查看/修改业务授权<${row.corporationName}>`;
      this.appFunctionListConfig.hbcClientAuthId = row.id;
      this.appFunctionListConfig.hbcClientAuthIdFixed=true;
      this.$refs.appFunctionListRef.getHbcClientFunctionListAll();// XXX TODO 直接触发子列表刷新
    },

    //==== License视图
    handleViewOrUpdateLicense(row){
      // // XXX TODO subview
      // console.log("handleViewOrUpdateLicense:",row);
      this.appLicenseListConfig.openLicense = true;
      this.appLicenseListConfig.titleLicense = `编辑/查看客户授权License<${row.corporationName}>`;
      this.appLicenseListConfig.hbcClientAuthId = row.id;
      this.appLicenseListConfig.queryParams.hbcClientAuthId=row.id;
      this.$refs.appLicenseListRef.getListLicense();// XXX TODO 直接触发子列表刷新
    },

  },// method

};
</script>
